﻿<?xml version="1.0" encoding="utf-8" ?>
<root>
  <table name="kp_ServiceAccounts">
    <column name="ServerName" datatype="varchar(50)" />
    <column name="dtmStamp" datatype="datetime" />
    <column name="ServiceAcct" datatype="varchar(500)" />
    <column name="AgentAcct" datatype="varchar(500)" />
    <column name="GUID" datatype="varchar(50)" />
  </table>
  <sql>
    <![CDATA[
DECLARE @SQLService   VARCHAR(60),
@AgentService VARCHAR(60),
@keyname      VARCHAR(255),
@agentkeyname VARCHAR(255)

SELECT @keyname =      CASE WHEN CHARINDEX('\',@@SERVERNAME,1) <> 0
THEN 'SYSTEM\ControlSet001\Services\MSSQL$' + @@SERVICENAME
ELSE 'SYSTEM\ControlSet001\Services\MSSQLSERVER'
END,

@agentkeyname = CASE WHEN CHARINDEX('\',@@SERVERNAME,1) <> 0
THEN 'SYSTEM\ControlSet001\Services\SQLAGENT$' + @@SERVICENAME
ELSE 'SYSTEM\ControlSet001\Services\SQLSERVERAGENT'
END

EXEC xp_regread @root_key   ='HKEY_LOCAL_MACHINE',
@key        = @keyname,
@valuename  = 'ObjectName',
@value    = @SQLService OUTPUT

EXEC xp_regread @root_key   = 'HKEY_LOCAL_MACHINE',
@key     = @agentkeyname,
@valuename  = 'ObjectName',
@value    = @AgentService OUTPUT

SELECT @@SERVERNAME, getdate(), @SQLService,@AgentService

        ]]>
</sql>

</root>